import { v2, EventTouch, EventMouse } from 'cc';
import { _decorator, Component, Node } from 'cc';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';
const { ccclass, property } = _decorator;
const tmpP2_0 = v2();
const tmpP2_1 = v2();
const tmpP2_2 = v2();

@ccclass('PinchGesture')
export class PinchGesture extends Component {

    protected onLoad(): void {
        this.initGesture();
        this.initMouse();
    }
    protected onEnable(): void {

    }
    protected onDisable(): void {

    }
    protected update(dt: number): void {

    }

    //#region --- 手势识别

    initGesture() {
        this.node.on(Node.EventType.TOUCH_START, this.onPinchStart, this);
        this.node.on(Node.EventType.TOUCH_MOVE, this.onPinchMove, this);
        this.node.on(Node.EventType.TOUCH_END, this.onPinchEnd, this);
        this.node.on(Node.EventType.TOUCH_CANCEL, this.onPinchEnd, this);

    }
    resetGesture() {

    }
    //#region --- 双指捏合
    _pinchScale: number = 0;
    _pinchTouchID = 0;
    _isPinchStart: boolean = false;
    _pinchCurLen: number = 0;
    _pinchStartLen: number = 0;
    _pinchCount = 0;

    onPinchStart(e: EventTouch) {
        const touchs = e.getAllTouches();
        this._pinchCount++;

        if (this._pinchCount > 2) return;

        if (this._pinchCount == 2 && touchs[0] && touchs[1]) {
            this._isPinchStart = true;
            this._pinchTouchID = e.getID();
            this._pinchScale = 1;
            // 计算长度
            touchs[0].getUILocation(tmpP2_0);
            touchs[1].getUILocation(tmpP2_1);

            this._pinchCurLen = tmpP2_1.subtract(tmpP2_0).length();
            this._pinchStartLen = this._pinchCurLen;

            // console.log('onPinchStart', this._pinchCurLen);
            // EventManager.emit(EventTypes.HomeSceneEvents.PinchGestureStart, this._pinchScale, e);
        }
    }
    // 
    onPinchMove(e: EventTouch) {
        if (!this._isPinchStart) return;

        const touchs = e.getAllTouches();

        if (this._pinchTouchID == e.getID() &&
            touchs[0] && touchs[1]) {
            // 计算长度
            touchs[0].getUILocation(tmpP2_0);
            touchs[1].getUILocation(tmpP2_1);

            const curLen = tmpP2_1.subtract(tmpP2_0).length();
            const subLen = curLen - this._pinchCurLen;
            this._pinchCurLen = curLen;
            this._pinchScale = this._pinchCurLen / this._pinchStartLen;

            // console.log('onPinchMove', subLen, this._pinchScale);
            // EventManager.emit(EventTypes.HomeSceneEvents.PinchGestureMove, subLen, e);
        }

    }

    onPinchEnd(e: EventTouch) {
        this._pinchCount--;

        if (this._pinchCount == 0) {
            // console.log('onPinchEnd', this._pinchScale);
            // this._isPinchStart &&
                // EventManager.emit(EventTypes.HomeSceneEvents.PinchGestureEnd, this._pinchScale, e);
            this._isPinchStart = false;
        }
    }
    //#endregion

    //#endregion

    //#region --- Web端 鼠标事件
    initMouse() {
        // 监听鼠标滚轮
        this.node.on(Node.EventType.MOUSE_WHEEL, this.onMouseWheel, this);
    }
    scrollYStep = 30;
    onMouseWheel(e: EventMouse) {
        const distY = e.getScrollY();
        const subLen = (distY > 0 ? 1 : -1) * this.scrollYStep;
        // EventManager.emit(EventTypes.HomeSceneEvents.PinchGestureMove, subLen, e);

    }
    //#endregion
}


